我們也可以透過json來製作api
@http.router('/book',metho=['POST'],type='json',auth='*',csrf=False)
def create_book(self, **kwargs):
data = request.jsonrequest
book_name = data.get('name')
author = data.get('author')
response = {
'message': 'Book created successfully',
'book_name': book_name,
'author': author
}
return response
@http.router('/book',metho=['POST'],type='json',auth='*',csrf=False)
def create_book(self, **kwargs):
data = http.request.jsonrequest
book_name = data.get('name')
author = data.get('author')
result = {'author': author}
response = json.dumps(result, default=data_utils.json_default)
return response
website=True
當我們在@http.router中加入website=True
之後,我們將會直接套用odoo的網頁主題:
'depends': [..., 'website'],
之後我們在先前的程式碼加入website=True
:
@http.route('/my_library/books', auth='user',website=True)
def list(self, **kwargs):
book = http.request.env['library.book']
books = obj.sudo().search([])
return http.request.render('library.library_template',{'books': books})